package com.cn.transfer.jobhandler.purchasearrival;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.VvPuPurchaseArrival;
import com.cn.transfer.common.entity.VvPuPurchaseArrivalB;
import com.cn.transfer.common.entity.VvSaSaleDelivery;
import com.cn.transfer.common.entity.VvSaSaleDeliveryB;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.handle.impl.purchase.community.PurchaseCommunity;
import com.cn.transfer.mapper.VvPuPurchaseArrivalBMapper;
import com.cn.transfer.mapper.VvPuPurchaseArrivalMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: weidian
 * @date: 2023/10/25 14:33
 * @Description: 进货单更新
 */
@Component
public class PurchaseArrivalUpdateXxlJob {
    @Autowired
    private VvPuPurchaseArrivalMapper vvPuPurchaseArrivalMapper;
    @Autowired
    private VvPuPurchaseArrivalBMapper vvPuPurchaseArrivalBMapper;

    @XxlJob("PurchaseArrivalUpdate")
    public void purchaseArrivalUpdate(){
        List<VvPuPurchaseArrival> deliveryList=vvPuPurchaseArrivalMapper.byChangedateList();
        int i = 0;
        for (VvPuPurchaseArrival vvPuPurchaseArrival : deliveryList) {
            if (vvPuPurchaseArrivalMapper.inquireIsExist(vvPuPurchaseArrival.getCode())==0){
                continue;
            }
            VvPuPurchaseArrival vvPuPurchaseArrival1 = vvPuPurchaseArrivalMapper.ById(vvPuPurchaseArrival.getId());   // 表单
            boolean b = ConnmeUtils.deepEquals(vvPuPurchaseArrival, vvPuPurchaseArrival1);
            if (!b) {
                if (vvPuPurchaseArrivalMapper.inquireIsExist(vvPuPurchaseArrival.getCode())>0){
                    // 不一致同步到视图和简道云
                    vvPuPurchaseArrivalMapper.deleteId(String.valueOf(vvPuPurchaseArrival.getId()));
                    vvPuPurchaseArrivalBMapper.deleteDetails(String.valueOf(vvPuPurchaseArrival.getId()));
                }
                Header[] headers = ConnmeUtils.createHeaders();
                Map<String, Object> parameterDelete = new HashMap<>();
                parameterDelete.put("app_id", "64f00ef131adb70007ad6e6d");
                parameterDelete.put("entry_id", "6535dbd4c1285112c61d4ca0");
                List<JdyPurchaseOrder> jdyPurchaseOrders = PurchaseCommunity.queryBased(parameterDelete, headers, vvPuPurchaseArrival.getCode(),"code");
                if (jdyPurchaseOrders.size() > 0) {
                    vvPuPurchaseArrivalMapper.insert(vvPuPurchaseArrival);
                    for (VvPuPurchaseArrivalB vvPuPurchaseArrivalB : vvPuPurchaseArrival.getArrival_list()) {
                        vvPuPurchaseArrivalBMapper.insert(vvPuPurchaseArrivalB);
                    }
                    Map<String, Object> parameterInster = new HashMap<>();
                    parameterInster.put("app_id", "64f00ef131adb70007ad6e6d");
                    parameterInster.put("entry_id", "6535dbd4c1285112c61d4ca0");
                    parameterInster.put("data_id", jdyPurchaseOrders.get(0).get_id());
                    parameterInster.put("data",ConnmeUtils.convertEntityToMap(vvPuPurchaseArrival));
                    String jsonSale = JSONUtil.toJsonStr(parameterInster);
                  ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSale);
                    i++;
                }
            }
        }

    }
}
